package com.tc.config;

import com.tc.interceptor.CheckTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private CheckTokenInterceptor checkTokenInterceptor;

@Override
public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .allowedHeaders("*")
            .allowCredentials(true);
}
    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(checkTokenInterceptor)
//                .addPathPatterns("/**")//拦截所有请求
//                .excludePathPatterns(
//                        "/login", "/register", // 排除登录和注册接口
//                        "/swagger-ui.html",
//                        "/swagger-ui/**",
//                        "/swagger-resources/**",
//                        "/v2/api-docs",
//                        "/webjars/**",
//                        "/swagger-resources/**",
//                        "/v2/**",
//                        "/swagger-ui.html/**",
//                        "/doc.html/**",
//                        "/doc.html",
//                        "/v3/api-docs/**",
//                        "/favicon.ico",
//                        "/error" // 排除Swagger相关的路径
//
//                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}